package com.fzy.core.codetools;

import java.io.OutputStream;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.naming.NamingException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class Dbo {
	private static final Log log = LogFactory.getLog(Generator.class);
	private Connection con;
	private Statement stm;
	private ResultSet rs;

	private Connection getCon() {

		try {
			Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
			String url = "jdbc:oracle:thin:@10.169.100.21:1521:inrdev";
			String user = "crmt3";
			String password = "crmt3";
//			Class.forName("org.h2.Driver").newInstance();
//			String url = "jdbc:h2:tcp://localhost/~/test";
//			String user = "sa";
//			String password = "";
			con = DriverManager.getConnection(url, user, password);
		} catch (SQLException e) {
			log.error("DBO -> getConnection SQLException errorMessage :"
					+ e.getMessage());
		} catch (Exception e) {
			log.error("DBO -> getConnection Exception errorMessage :"
					+ e.getMessage());
		}
		return con;
	}

	private Statement getStm() {
		Connection con = getCon();
		try {
			stm = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
					ResultSet.CONCUR_READ_ONLY);
		} catch (Exception e) {
			log.error("创建Statement对象失败！");
			e.printStackTrace();
		}
		return stm;
	}

	public ResultSet query(String sql) {
		if (sql == null)
			sql = "";
		try {
			getStm();
			rs = stm.executeQuery(sql);
		} catch (Exception e) {
			log.error("执行查询方法失败！");
			e.printStackTrace();
		}
		return rs;
	}

	public int insert(String sql) {
		int i = -1;
		if (sql == null)
			sql = "";
		log.error("sql:" + sql);
		try {
			getStm();
			i = stm.executeUpdate(sql);
		} catch (Exception e) {
			i = -1;
			e.printStackTrace();
		}
		return i;
	}

	public int update(String sql) {
		int i = -1;
		if (sql == null)
			sql = "";
		try {
			getStm();
			i = stm.executeUpdate(sql);
		} catch (Exception e) {
			i = -1;
			e.printStackTrace();
		}
		return i;
	}

	public int delete(String sql) {
		int i = -1;
		if (sql == null)
			sql = "";
		try {
			getStm();
			i = stm.executeUpdate(sql);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return i;
	}

	public void close() {
		try {
			if (rs != null)
				rs.close();
			if (stm != null)
				stm.close();
			if (con != null)
				try {
					con.close();
				} catch (SQLException e) {
					log.error("DBO -> releaseConnection SQLException errorMessage :"
							+ e.getMessage());
				} catch (Exception e) {
					log.error("DBO -> releaseConnection Exception errorMessage :"
							+ e.getMessage());
				}
		} catch (SQLException e) {
			log.error("关闭数据库连接失败！");
			e.printStackTrace();
		}
	}

	public static OutputStream getBlobOutputStream(Blob blob) {
		OutputStream o = null;
		// try {
		// o = blob == null ?
		// null:((oracle.sql.BLOB)blob).getBinaryOutputStream();
		// } catch (SQLException e) {
		// Log.error("getBlobOutputStream ->" + e.getMessage());
		// e.printStackTrace();
		// }
		return o;
	}
}
